package com.baizhi.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.util.PoiPublicUtil;
import com.baizhi.entity.Article;
import com.baizhi.entity.Guru;
import com.baizhi.service.ArticleService;
import com.baizhi.service.GuruService;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;


@Controller
@RequestMapping("guru")
public class GuruController {
    @Autowired
    private GuruService guruService;
    @Autowired
    private ArticleService articleService;
    @ResponseBody
    @RequestMapping("selectGuru")
    public List selectGuru(){
        List<Guru> gurus = guruService.selectAllGuru();
        return gurus;
    }
    @ResponseBody
    @RequestMapping("selectAllGuru")
    public Map selectAllGuru(String name , Integer page, Integer limit){
        Map map = new HashMap();
        PageInfo<Guru> pageInfo = guruService.selectAll(name, page, limit);
        map.put("code",0);
        map.put("msg","");
        map.put("count",pageInfo.getTotal());
        map.put("data",pageInfo.getList());
        return map;
    }
    @ResponseBody
    @RequestMapping("selectOneGuru")
    public Guru selectOneGuru(Integer id){
        Guru guru = guruService.selectOneGuru(id);
        return guru;
    }
    @ResponseBody
    @RequestMapping("insertGuru")
    public Map insertGuru(Guru guru){
        Map map = new HashMap();
        Boolean isAdd = guruService.insertGuru(guru);
        System.out.println("guru = " + guru+"----"+isAdd);
        map.put("isAdd",isAdd);
        return map;
    }
    @ResponseBody
    @RequestMapping("updateGuru")
    public Map updateGuru(Guru guru){
        Map map = new HashMap();
        Boolean isUpdate = guruService.updateGuru(guru);
        System.out.println("guru = " + guru);
        System.out.println("isUpdate = " + isUpdate);
        map.put("isUpdate",isUpdate);
        return map;
    }
    @ResponseBody
    @RequestMapping("deleteGuru")
    public Map deleteGuru(Guru guru){
        Map map = new HashMap();
        Article article = new Article();
        if(guru.getStatus()==0){
            guru.setStatus(1);
            article.setStatus(2);
            article.setId(guru.getId());
            articleService.updateGid(article);
        }else{
            guru.setStatus(0);
            article.setStatus(1);
            article.setId(guru.getId());
            articleService.updateGid(article);
        }
        Boolean isDelete = guruService.updateGuru(guru);
        map.put("isDelete",isDelete);
        return map;
    }
    @ResponseBody
    @RequestMapping("upload")
    public Map upload(MultipartFile file) throws Exception {
        System.out.println("file = " + file);
        Map map = new HashMap();
        InputStream inputStream = file.getInputStream();
        ImportParams params = new ImportParams();
        params.setTitleRows(1);
        params.setHeadRows(1);
        List<Guru> list = ExcelImportUtil.importExcel(inputStream,
                Guru.class, params);
        System.out.println(list.size());
        for (Guru guru : list) {
            guru.setId(null);
        }
        guruService.insertAll(list);

        return map;
    }
    @ResponseBody
    @RequestMapping("download")
    public void download(HttpServletResponse response) throws Exception {
        Map map = new HashMap();
        System.out.println("\"下载\" = " + "下载");
        List<Guru> gurus = guruService.selectAllGuru();
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("上师数据展示","学生"),
                Guru.class, gurus);

        response.setHeader("Content-Disposition",
                "attachment;fileName=" + URLEncoder.encode("guru.xlsx", "utf-8"));
        response.setContentType("application/vnd.ms-excel");
        workbook.write(response.getOutputStream());

//        return map;
    }
}
